package com.deyuanyun.pic.settings.controller.pipe;

import com.deyuanyun.pic.common.dto.PageJsonBean;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.common.util.ajax.AjaxSupport;
import com.deyuanyun.pic.common.util.web.Asserts;
import com.deyuanyun.pic.settings.controller.AbstractBaseController;
import com.deyuanyun.pic.settings.controller.dto.PipeLineInfraVO;
import com.deyuanyun.pic.settings.controller.dto.PipeSegmentInfraVO;
import com.deyuanyun.pic.settings.controller.dto.PipeValidateVO;
import com.deyuanyun.pic.settings.controller.dto.PipeValveRoomInfraVO;
import com.deyuanyun.pic.settings.enumeration.PipeValidateType;
import com.deyuanyun.pic.settings.service.pipe.PipeLineElementService;
import com.deyuanyun.pic.settings.service.pipe.ValveRoomService;
import com.deyuanyun.pic.settings.support.biz.CodeUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.MessageFormat;

/**
* @ClassName: BasucSettingsController 
* @Description: 基础设置Controller
* @author h.x.rong
* @date  2015年12月23日
*
 */
@Controller
@RequestMapping("/settings/pipe")
public class BasicSettingsController extends AbstractBaseController {

	@Resource(name="valveRoomServiceImpl")
	ValveRoomService valveRoomService;

    @Resource(name = "PipeLineElementSettingService")
    PipeLineElementService pipeLineElementService;

    private static final String DELETE_STR = "已选择{0}条数据，成功删除{1}条数据"; //删除数据返回的字符串

    private static final String RELEASE_STR = "已选择{0}条数据，成功发布{1}条数据"; //发布数据返回的字符串

    private static final String DIS_ENA_STR = "已选择{0}条记录，成功{1}{2}条数据"; //停用或者启用返回的字符串

	/**
	 * 跳转到管道基础设置页面
	 * @param modelMap
	 * @return
	 * @author axiao(xj08.dream@gmail.com)
	 * @date 2016-09-01
	 */
	@RequestMapping("/toPipeValveroomMgr.html")
	public String toPipeValveRoom(ModelMap modelMap){
		//初始化页面值
        modelMap.addAttribute("valveRoomType",CodeUtil.getCodeList("SITE_TYPE"));
        return "/settings/base/pipeManage";
	}


    //*********************** 场站阀室 begin *****************************

    /**
     * @Description: 场站阀室条件查询
     * @author h.x.rong
     * @date 2015年12月26日
     * @param @param pipeValveRoomInfraVO
     * @param @return
     * @return ModelAndView
     *
     */
    @RequestMapping("/findByConditinForVR.json")
    @ResponseBody
    public void findByConditinForVR(PipeValveRoomInfraVO pipeValveRoomInfraVO){
        PageJsonBean jsonBean = new PageJsonBean();
        jsonBean.setList(valveRoomService.findByCondition(pipeValveRoomInfraVO));
        AjaxSupport.sendSuccessText("success",jsonBean);
    }

    /**
     * 管道基础设置验证名称和代号是否重复
     * @param pipeValidateVO
     * @return 
     * @author axiao(xj08.dream@gmail.com)
     * @date 2016-11-25
     */
    @RequestMapping("/validateRepeat.json")
    @ResponseBody
    public void validateRepeat(PipeValidateVO pipeValidateVO) {
        String type = pipeValidateVO.getType();
        boolean f = false;
        if(PipeValidateType.line.toString().equals(type)) {
            f = lineService.validateRepByline(pipeValidateVO);
        } else if (PipeValidateType.segment.toString().equals(type)) {
            f = segmentService.validateRepBySegment(pipeValidateVO);
        }else if (PipeValidateType.valve.toString().equals(type)) {
            f = valveRoomService.validateRepByVR(pipeValidateVO);
        }
        AjaxSupport.sendSuccessText("success", f);
    }

	
	/**
	* @Description: 新增场站阀室
	* @author h.x.rong
	* @date 2015年12月25日
	* @param
	* @return
	 */
	@RequestMapping("/addPipeValveRoom.json")
	@ResponseBody
	public void addPipeValveRoom(PipeValveRoomInfraVO pipeValveRoomInfraVO){
        boolean f = valveRoomService.validateRepByVR(new PipeValidateVO().setName(pipeValveRoomInfraVO.getValveRoomName())
                .setAbridge(pipeValveRoomInfraVO.getValveRoomAbridge()));
		if(f){
			AjaxSupport.sendFailText("fail","场站阀室名称或场站阀室代号数据重复，请修改");
		}else{
			valveRoomService.insertValveRoom(pipeValveRoomInfraVO);
			AjaxSupport.sendSuccessText("success", "新增阀室成功");
		}
	}

	/**
	 * 批量删除未发布的场站阀室
	 * @param
	 * @return
	 * @author axiao(xj08.dream@gmail.com)
	 * @date 2016-09-01
	 */
	@RequestMapping("/removeValveRoom.json")
    @ResponseBody
	public void removeValveRoom(String[] ids){
        if(ids == null) {
            AjaxSupport.sendSuccessText("success", MessageFormat.format(DELETE_STR, 0, 0));
            return;
        }
		int n = valveRoomService.deleteByIds(ids);
		AjaxSupport.sendSuccessText("success", MessageFormat.format(DELETE_STR, ids.length, n));
	}

	/**
	 * 场站阀室批量发布
	 * @param
	 * @return 
	 * @author axiao(xj08.dream@gmail.com)
	 * @date 2016-09-01
	 */
	@RequestMapping("/updateVRstated.json")
	@ResponseBody
	public void updateVRstated(String[] ids){
        if(ids == null) {
            AjaxSupport.sendSuccessText("success", MessageFormat.format(RELEASE_STR, 0, 0));
            return;
        }
        int n = valveRoomService.updateStatus(ids);
		AjaxSupport.sendSuccessText("success", MessageFormat.format(RELEASE_STR, ids.length, n));
	}


	/**
	 * 场站阀室数据状态修改
	 * @param
	 * @return 
	 * @author axiao(xj08.dream@gmail.com)
	 * @date 2016-09-01
	 */
	@RequestMapping("/updateVRS.json")
	@ResponseBody
	public void updateVRS(String[] ids,String status){
		String stateFlag = StringUtils.isNotEmpty(status) && "1".equals(status) ? "停用" : "启用";
		if(ids == null || StringUtils.isEmpty(status))
            AjaxSupport.sendFailText("success", MessageFormat.format(DIS_ENA_STR, 0, stateFlag, 0));
		int n =valveRoomService.updateVRS(ids,status);
		if(n > 0) { //大于零的时候清空缓存
            AjaxSupport.sendSuccessText("success", MessageFormat.format(DIS_ENA_STR, ids.length, stateFlag, n));
		} else {
            AjaxSupport.sendFailText("fail", MessageFormat.format(DIS_ENA_STR, ids.length, stateFlag, n));
        }
	}


	/**
	* @Description:修改站场阀室名称及阀室代号
	* @author h.x.rong
	* @date 2015年12月30日
	* @param 
	* @return void 
	*
	 */
	@RequestMapping("/updateVR.json")
	@ResponseBody
	public void updateVR(PipeValveRoomInfraVO pipeValveRoomInfraVO){
		boolean f = valveRoomService.validateRepByVR(new PipeValidateVO().setName(pipeValveRoomInfraVO.getValveRoomName())
                .setAbridge(pipeValveRoomInfraVO.getValveRoomAbridge()).setId(pipeValveRoomInfraVO.getId()));
		if(f){
			AjaxSupport.sendFailText("fail","场站阀室名称或场站阀室代号数据重复，请修改");
		}else{
			valveRoomService.updateVR(pipeValveRoomInfraVO);
			AjaxSupport.sendSuccessText("success","修改成功");
		}
	}

    //*********************** 场站阀室 end *****************************


    //*********************** 管道 begin *****************************


    /**
     * @Description: 管道数据初始化值显示
     * @author h.x.rong
     * @date 2016年1月4日
     * @param @param pipeLineInfraVO
     * @return void
     *
     */
    @RequestMapping("/findByConditinForLine.json")
    @ResponseBody
    public void findByConditinForLine(PipeLineInfraVO pipeLineInfraVO){
        PageJsonBean jsonBean = new PageJsonBean();
        jsonBean.setList(lineService.selectByCondition(pipeLineInfraVO));
        AjaxSupport.sendSuccessText("success",jsonBean);
    }

    /**
     * 查找管道组成元素列表
     * @param pipeLineId 管道id
     * @param type 为1是查询地面标识，为0查询场站阀室, 为空时查询全部
     * @return 
     * @author axiao(xj08.dream@gmail.com)
     * @date 2016-09-03
     */
    @RequestMapping("/findElements.json")
    @ResponseBody
    public void findElements(String pipeLineId, String type) {
        AjaxSupport.sendSuccessText("success", pipeLineElementService.queryElementsById(pipeLineId, type));
    }

    /**
     * 根据管道要素id数组，进行是否用于分段操作
     * @param ids 需要启用、停用的管道要素id
     * @param status 0为否，1为是
     * @throws
     * @author axiao(xj08.dream@gmail.com)
     * @date 2016-09-03
     */
    @RequestMapping("/updateElements.json")
    @ResponseBody
    public void updateElements(String ids, String status, @RequestParam(value = "mileage", required = false) BigDecimal mileage) {
        String stateFlag = StringUtils.isEmpty(status) && "1".equals(status) ? "停用" : "启用";
        if(ids == null || StringUtils.isEmpty(status)){
            AjaxSupport.sendFailText("fail", MessageFormat.format(DIS_ENA_STR, 0, stateFlag , 0));
            return;
        }
        int n = pipeLineElementService.updateElements(ids,status, mileage);
        if(n > 0) {
            AjaxSupport.sendSuccessText("success", MessageFormat.format(DIS_ENA_STR, 1, stateFlag, n));
        } else {
            AjaxSupport.sendFailText("fail", MessageFormat.format(DIS_ENA_STR, 0, stateFlag, n));
        }

    }

    /**
     * 场站阀室下拉框数据，允许使用名字搜索
     * @param signName 场站阀室名称
     * @return 
     * @author axiao(xj08.dream@gmail.com)
     * @date 2016-09-05
     */
    @RequestMapping("/queryAllValueRooms.json")
    @ResponseBody
    public void queryAllValueRooms(String signName) {
		PageJsonBean jsonBean = new PageJsonBean(1, 1000);
        jsonBean.setList(valveRoomService.queryAllValueRooms(signName));
        AjaxSupport.sendSuccessText("success", jsonBean, "id", "name");
    }


	/**
	 * 新增管道
	 * @param lineName 管道名称
     * @param lineAbridge 管道代号
     * @param vrIds 场站阀室id数组
	 * @return
	 * @author axiao(xj08.dream@gmail.com)
	 * @date 2016-09-03
	 */
	@RequestMapping("/addPipeLine.json")
	@ResponseBody
	public void addPipeLine(String lineName, String lineAbridge, String[] vrIds){
		Asserts.assertTrue(vrIds.length > 1, "沿线场站阀室必须有两个以上！");
        Asserts.assertTrue(StringUtils.isNotEmpty(lineName) || StringUtils.isNotEmpty(lineAbridge), "请输入必填项");
		lineName = lineName.trim();
		lineAbridge = lineAbridge.trim();
		boolean f = lineService.validateRepByline(new PipeValidateVO().setName(lineName).setAbridge(lineAbridge));
        if(f) {
			AjaxSupport.sendFailText("fail", "管道名称或管道代号数据重复，请修改");
		} else {
			 lineService.insertPipeLineInfo(lineName, lineAbridge, vrIds);
			AjaxSupport.sendSuccessText("success", "新增管道成功");
		}
	}


    /**
     * 删除未发布的管道信息
     * @param ids
     * @return
     * @author axiao(xj08.dream@gmail.com)
     * @date 2016-09-02
     */
	@RequestMapping("/removeLineByIds.json")
    @ResponseBody
	public void removeLineById(String[] ids){
        if(ids == null) {
            AjaxSupport.sendFailText("fail", MessageFormat.format(DELETE_STR, 0, 0));
            return;
        }
        int n = lineService.deleteLineByIds(ids);
		AjaxSupport.sendSuccessText("success", MessageFormat.format(DELETE_STR, ids.length, n));
	}

    /**
     * 修改管道数据
     * @param pipeLineInfraVO
     * @param vrIds 管道的选择的场站阀室id数组
     * @return
     * @author axiao(xj08.dream@gmail.com)
     * @date 2016-09-03
     */
	@RequestMapping("/updateLine.json")
	@ResponseBody
	public void updateLine(PipeLineInfraVO pipeLineInfraVO, String[] vrIds){
        boolean f = lineService.validateRepByline(new PipeValidateVO().setName(pipeLineInfraVO.getLineName()).setAbridge(pipeLineInfraVO.getLineAbridge()).setId(pipeLineInfraVO.getId()));
		if(f){
			AjaxSupport.sendFailText("fail","管道名称或管道代号数据重复，请修改");
		}else{
			lineService.updatePipeLineInfo(pipeLineInfraVO, vrIds);
			AjaxSupport.sendSuccessText("success", "修改成功");
		}
	}
	
    /**
     * 管道数据发布
     * @param ids
     * @return 
     * @author axiao(xj08.dream@gmail.com)
     * @date 2016-09-02
     */
	@RequestMapping("/updateLineStated.json")
	@ResponseBody
	public void updateLineStated(String[] ids){
		if(ids == null) {
            AjaxSupport.sendFailText("fail", MessageFormat.format(RELEASE_STR, 0, 0));
            return;
        }
		int n = lineService.updateStatus(ids);
		AjaxSupport.sendSuccessText("success", MessageFormat.format(RELEASE_STR, ids.length, n));
	}


	/**
	 * 管道启用、停用数据修改
	 * @param ids
     * @param status 0为启用，1为停用
	 * @return
	 * @author axiao(xj08.dream@gmail.com)
	 * @date 2016-09-02
	 */
	@RequestMapping("/updateLineS.json")
	@ResponseBody
	public void updateLineS(String[] ids,String status){
        String stateFlag = StringUtils.isEmpty(status) && "1".equals(status) ? "停用" : "启用";
        if(ids == null)
            AjaxSupport.sendFailText("fail", MessageFormat.format(DIS_ENA_STR, 0, stateFlag, 0));
        int n = lineService.updateLineS(ids, status);
        if(n > 0) { //大于零的时候清空缓存
            AjaxSupport.sendSuccessText("success", MessageFormat.format(DIS_ENA_STR, ids.length, stateFlag, n));
        } else {
            AjaxSupport.sendFailText("fail", MessageFormat.format(DIS_ENA_STR, ids.length, stateFlag, n));
        }
	}

    //*********************** 管道 end *****************************

    //*********************** 管段 begin *****************************

    /**
     * 管段初始化：查询、条件查询
     * @param
     * @return
     * @throws
     * @author axiao(xj08.dream@gmail.com)
     * @date 2016-09-02
     */
    @RequestMapping("/findByConditionForSegment.json")
    @ResponseBody
    public void findByConditionForSegment(PipeSegmentInfraVO pipeSegmentInfraVO){
        PageJsonBean jsonBean=new PageJsonBean();
        jsonBean.setList(segmentService.findByCondition(pipeSegmentInfraVO));
        AjaxSupport.sendSuccessText("success",jsonBean);
    }

	/**
	* @Description: 管段初始化：新增管段
	* @author h.x.rong
	* @date 2016年1月5日
	* @param 
	* @return void 
	*
	 */
	@RequestMapping("/addPipeSegment.json")
	@ResponseBody
	public void addPipeSegment(PipeSegmentInfraVO pipeSegmentInfraVO){
        boolean  f = segmentService.validateRepBySegment(new PipeValidateVO().setName(pipeSegmentInfraVO.getSegmentName())
                .setAbridge(pipeSegmentInfraVO.getSegmentAbridge()));
        if(f){
			AjaxSupport.sendFailText("fail","管段名称或者管段代号重复，请修改");
		}else{
			 segmentService.insert(pipeSegmentInfraVO);
			AjaxSupport.sendSuccessText("success", "新增管段成功");
		}
	}
	
	/**
	 * 管段批量删除
	 * @param ids
	 * @return 
	 * @author axiao(xj08.dream@gmail.com)
	 * @date 2016-09-02
	 */
	@RequestMapping("/removeSegmentByIds.json")
	@ResponseBody
	public void removeSegmentById(String[] ids){
        if(ids == null) {
            AjaxSupport.sendSuccessText("success", MessageFormat.format(DELETE_STR, 0, 0));
            return;
        }
        int n = segmentService.deleteSegmentById(ids);
        AjaxSupport.sendSuccessText("success", MessageFormat.format(DELETE_STR, ids.length, n));
	}

	/**
	* @Description: 管道初始化：根据id修改数据
	* @author h.x.rong
	* @date 2016年1月5日
	* @param 
	* @return void 
	*
	 */
	@RequestMapping("/updatePipeSegment.json")
	@ResponseBody
	public void updatePipeSegment(PipeSegmentInfraVO pipeSegmentInfraVO){
        boolean  f = segmentService.validateRepBySegment(new PipeValidateVO().setName(pipeSegmentInfraVO.getSegmentName())
                .setAbridge(pipeSegmentInfraVO.getSegmentAbridge()).setId(pipeSegmentInfraVO.getId()));
        if(f){
			AjaxSupport.sendFailText("fail","管段名称或者管段代号重复，请修改");
		}else{
            segmentService.updatePipeSegment(pipeSegmentInfraVO);
			AjaxSupport.sendSuccessText("success", "管段修改成功");
		}
	}
	
    /**
     * 管段发布
     * @param ids 管段id数组
     * @return 
     * @author axiao(xj08.dream@gmail.com)
     * @date 2016-09-02
     */
	@RequestMapping("/updateSegmentStated.json")
	@ResponseBody
	public void updateSegmentStated(String[] ids){
        if(ids == null) {
            AjaxSupport.sendSuccessText("success", MessageFormat.format(RELEASE_STR, 0, 0));
            return;
        }
        int n = segmentService.updateStatus(ids);
		AjaxSupport.sendSuccessText("success", MessageFormat.format(RELEASE_STR, ids.length, n));
	}

    /**
     * 管段数据状态，启用停用修改
     * @param ids
     * @param status 0为启用，1为停用
     * @return
     * @author axiao(xj08.dream@gmail.com)
     * @date 2016-09-01
     */
	@RequestMapping("/updateSegmentS.json")
	@ResponseBody
	public void updateSegmentS(String[] ids,String status){
        String stateFlag = StringUtils.isEmpty(status) && "1".equals(status) ? "停用" : "启用";
        if(ids == null)
            AjaxSupport.sendSuccessText("success", MessageFormat.format(DIS_ENA_STR, 0, stateFlag, 0));
        int n = segmentService.updateSegmentS(ids, status);
        AjaxSupport.sendSuccessText("success", MessageFormat.format(DIS_ENA_STR, ids.length, stateFlag, n));
	}

    //*********************** 管段 begin *****************************
	
}
